package xyz.lisys.boot.service;

import lombok.val;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.lisys.boot.mapper.UserMapper;
import xyz.lisys.boot.pojo.User;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class UserService {

    @Autowired(required = false)
    public RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public UserMapper userMapper;

    public List<User> findAllUser() {
        if (redisTemplate != null) {
            Integer count = (Integer) Optional.ofNullable(redisTemplate.opsForValue().get("count")).orElse(0);
            redisTemplate.opsForValue().set("count", ++count);
        }
        return userMapper.findAllUser();
    }

    @Transactional
    public void insertUser() {
        val user = new User();
        user.setName("aa");
        user.setPhone(RandomStringUtils.randomNumeric(11));
        user.setPassword("123456");
        userMapper.insertUser(user);
        if (RandomUtils.nextBoolean()) {
            val i = 1 / 0;
        }
    }
}
